package com.sdjzu.wangfuying.hellospringboot.controller;

import com.sdjzu.wangfuying.hellospringboot.annotation.LoginRequired;
import com.sdjzu.wangfuying.hellospringboot.bean.db.Student;
import com.sdjzu.wangfuying.hellospringboot.service.StudentService;
import com.sdjzu.wangfuying.hellospringboot.util.SpringBootUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class ApiController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring !";
    }

    @LoginRequired
    @RequestMapping("/save")
    public String save() {
        Student student = new Student();
        student.setId((int) (System.currentTimeMillis() / 10000));
        student.setName(new Date().toString());
        studentService.save(student);
        SpringBootUtil.getRequest().getSession().setAttribute("lastUpdateTime", System.currentTimeMillis());
        return "ok";
    }

    @RequestMapping("/findAll")
    public Iterable<Student> findAll() {
        SpringBootUtil.getRequest().getSession().setAttribute("lastUpdateTime", System.currentTimeMillis());
        return studentService.findAll();
    }

    @RequestMapping("/login")
    public String login() {
        SpringBootUtil.getRequest().getSession().setAttribute("token", UUID.randomUUID());
        return "ok";
    }
}
